//
//  RestaurantReviewCollectionViewCell.swift
//  FeastBooking
//
//  Created by Imon on 2017/11/19.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

class RestaurantReviewCollectionViewCell: UICollectionViewCell {
    
    var avatar: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.image = UIImage(named: "placeholder")
        return imageView
    }()
    
    let username: UILabel = {
        let label = UILabel()
        label.textColor = darkTextColor
        label.textAlignment = .left
        label.font = AppFont(size: 12)
        return label
    }()
    
    let grade: GradeView = {
        let view = GradeView()
        view.initValue(grade: 0.0)
        return view
    }()
    
    let review: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 12)
        label.numberOfLines = 0
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let date: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.textAlignment = .left
        label.font = AppFont(size: 12)
        return label
    }()
    
    let saperator: UIView = {
        let view = UIView()
        view.backgroundColor = saperatorColor
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(avatar)
        self.addSubview(username)
        self.addSubview(grade)
        self.addSubview(review)
        self.addSubview(date)
        self.addSubview(saperator)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initValue(review: RestaurantReview) {
        if let username = review.author {
            self.username.text = username
        }
        if let rating = review.rating {
            self.grade.initValue(grade: CGFloat(rating))
        }
        if let review = review.review {
            self.review.text = review
        }
        if let date = review.reviewTime {
            self.date.text = date
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let gradeWidth: CGFloat = 150
        let gap: CGFloat = 10
        avatar.frame = CGRect(x: 15, y: 15, width: 20, height: 20)
        avatar.layer.cornerRadius = avatar.frame.width / 2
        avatar.layer.masksToBounds = true
        let maxSize = CGSize(width: self.contentView.bounds.width - 55, height: self.contentView.bounds.height)
        let nameSize = TextSize.textSize(text: username.text!, font: username.font, maxSize: maxSize)
        username.frame = CGRect(x: avatar.frame.origin.x + avatar.frame.width + 10, y: 20, width: nameSize.width, height: nameSize.height)
        grade.frame = CGRect(x: self.contentView.frame.width - 10 - gradeWidth, y: 15, width: gradeWidth, height: 20)
        let reviewSize = TextSize.textSize(text: review.text!, font: review.font, maxSize: maxSize)
        review.frame = CGRect(x: username.frame.origin.x, y: username.frame.origin.y + username.frame.height + gap, width: reviewSize.width, height: reviewSize.height)
        let dateSize = TextSize.textSize(text: date.text!, font: date.font, maxSize: maxSize)
        date.frame = CGRect(x: username.frame.origin.x, y: review.frame.origin.y + review.frame.height + gap, width: dateSize.width, height: dateSize.height)
        saperator.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(bounds.height - 0.5, 15, 0, 10))
    }
}
